﻿using System;
using System.Windows.Forms;
using CanvasControl.Interface;

namespace CanvasControl.View
{
    public partial class CanvasView : UserControl, ICanvasView
    {
        public CanvasController Controller { get; private set; }

        public CanvasView()
        {
            Controller = new CanvasController(this);
            InitializeComponent();

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.ContainerControl, true);

            Controller.OnSizeChanged(Size);
        }

        public void UpdateView()
        {
            Invalidate();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            var context = new DrawingContext(e.Graphics, e.ClipRectangle);
            Controller.DrawBackground(context);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var context = new DrawingContext(e.Graphics, e.ClipRectangle);
            Controller.DrawOther(context);
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            Controller.OnMouseWheel(e.Delta);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            Controller.OnMouseDown(e.Location, ModifierKeys, e.Button);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            Controller.OnMouseUp(e.Location);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            Controller.OnMouseMove(e.Location);
        }

        private void OnSizeChanged(object sender, EventArgs e)
        {
            Controller.OnSizeChanged(Size);
        }
    }
}
